package com.zbs.framework.security.handler;

import com.zbs.framework.common.utils.ResponseUtil;
import com.zbs.framework.common.utils.ResultUtil;
import com.zbs.framework.security.config.FrameworkConstants;
import com.zbs.framework.security.utils.TokenManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: Zbs
 * @createTime: 2021/2/25 11:23
 * 退出处理
 */
@Component
public class TokenLogoutHandle implements LogoutHandler {

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String token = request.getHeader(FrameworkConstants.TOKEN_NAME);
        if (token != null){
            String userName = TokenManager.getUserName(token);
            String entryType = TokenManager.getEntryType(token);
            //移除token
            TokenManager.removeToken(userName,entryType);
        }
        ResponseUtil.out(response,request, ResultUtil.ok());
    }
}
